﻿Imports System.IO

Namespace Logger

    Module mLogger

        Private LoggingStream As StreamWriter
        Private LoggingReader As StreamReader

        Private LogFile As String

        Private objLock As New Object

        Public Sub LogSearch(query As String, lines As Integer)

            Dim O As String = ""
            Dim C As Integer

            SyncLock objLock

                LoggingStream.Flush()
                LoggingStream.Close()
                LoggingStream.Dispose()

                System.Windows.Forms.Application.DoEvents()

                Dim tmp As New FileStream(LogFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)
                LoggingReader = New StreamReader(tmp)

                Dim L As String 'current line 
                Dim S As String 'current search text (for seperating */?)
                Dim WildCard As Boolean
                Dim SingleWildCard As Boolean
                Dim Start As Integer = 1

                'wildcard string
                Dim WC As String = ""

                Dim R As Boolean = True ' result

                'loop until end
                Do Until LoggingReader.EndOfStream

                    Start = 1

                    'read line to S
                    L = LoggingReader.ReadLine()
                    R = True

                    Do

                        If InStr(query, " ") = 0 Then S = query Else S = Mid(query, Start, query.Length - Start)

                        'reset wildcard flags
                        WildCard = False
                        SingleWildCard = False

                        'wildcard
                        If Mid(query, Start, 1) = "*" Then
                            WildCard = True
                            S = Mid(S, Start, InStr(Start, S, "*") - 1)
                            S = Right(S, S.Length - InStr(Start, S, "*"))
                            L = Right(L, L.Length - InStr(Start, L, "*"))
                        End If

                        'single wildcard
                        If Mid(query, Start, 1) = "?" Then
                            SingleWildCard = True
                            S = Mid(S, Start, InStr(Start, S, "?") - 1)
                            S = Right(S, S.Length - InStr(Start, S, "?"))
                            L = Right(L, L.Length - InStr(Start, L, "?"))
                            If Mid(S, Start, WC.Length) = WC Then R = True Else R = False
                        End If

                        Start += S.Length

                        If Not WildCard Then
                            Start -= 1
                            If Not LCase(L).Contains(LCase(S)) Then R = False
                        Else
                            If InStr(Start, L, S) > 0 Then R = True Else R = False
                        End If

                    Loop Until Not R Or Start >= S.Length

                    If R And InStr(LCase(L), ">logquery") = 0 And InStr(L, "> \B0\F8") = 0 And InStr(L, "> Log Query: \F6\B0") = 0 Then
                        Dim P As Integer = InStr(InStr(L, " ") + 1, L, " ")
                        Dim D As String
                        If P = 0 Then D = "no date!" Else D = Mid(L, InStr(L, " ") + 1, P - (InStr(L, " ") + 1))

                        P = InStr(L, "=> ")
                        If P = 0 Then S = L Else S = Right(L, (L.Length - P) - 2)

                        O &= "\F7  > \B0\F8" & D & "\F0\B8 " & S & vbCrLf

                        C += 1
                        If C = lines Then Exit Do

                    End If

                Loop


                LoggingReader.Close()
                LoggingReader.Dispose()

                LoggingStream = New StreamWriter(LogFile, True)
                
            End SyncLock

            Output.AddLine("\F5  > Log Query: \F6\B0" & query & ", " & C & " results \F0\B8")
            Output.AddLine(O)

        End Sub

        Public Sub StartLogger()

            Dim sLogsPath As String = Globals.sServerPath & "Logs\"

            If Not Directory.Exists(sLogsPath) Then
                Directory.CreateDirectory(sLogsPath)
            End If

            'Try

            Dim f As String = "Log-" & DateTime.Now.ToString & ".txt"
            f = Replace(f, "/", "-")
            f = Replace(f, ":", "-")

            If File.Exists(sLogsPath & f) = False Then
                ' Create a file to write to.
                Dim S As FileStream = New FileStream(sLogsPath & f, FileMode.OpenOrCreate, FileAccess.ReadWrite)
                LoggingStream = New StreamWriter(S)
                LoggingStream.WriteLine("MirEvoSvr Log File")
                LoggingStream.Flush()
                LoggingStream.Close()
            End If

            LoggingStream = File.AppendText(sLogsPath & f)

            Output.AddLine("\B0\F2LG>\F0\B8 Logger Started [\B0\F6" & f & "\B8\F0]")

            LogFile = sLogsPath & f

            'Catch

            'MsgBox("!!! Logger Error - !!! - Check '.\Logs\' Directory Exists - !!!", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Logger")
            'Globals.bShutdown = True

            'End Try

        End Sub

        Public Sub StopLogger()

            Output.AddLine("\B0\F2LG>\F0\B8 Stopping Logger @ " & DateTime.Now.ToString)

            LoggingStream.Flush()
            LoggingStream.Close()

        End Sub

        Public Sub LoggerWrite(txt As String)

            SyncLock objLock

                LoggingStream.Write(txt)
                LoggingStream.Flush()

            End SyncLock

        End Sub

        Public Sub LoggerWriteLine(txt As String)

            LoggerWrite("[" & DateTime.Now.ToString & " " & DateTime.Now.Millisecond & "ms]=> " & txt & vbCrLf)

        End Sub

    End Module

End Namespace